# -*- coding: utf-8 -*-
"""页面对象基类。
封装 find / click / type / get_text 等常用操作，子类页面直接继承。
"""

from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from common.log import log
from common.selenium import object_left_click, object_send_keys


class BasePage:
    def __init__(self, driver, timeout: int = 10):
        self.driver = driver
        self.timeout = timeout

    # ---------------- 基础封装 ----------------
    def find(self, locator):
        """显式等待元素出现并返回 WebElement"""
        return WebDriverWait(self.driver, self.timeout).until(
            EC.presence_of_element_located(locator)
        )

    def click(self, locator):
        """点击元素（封装日志与等待）"""
        log.debug(f"点击元素: {locator}")
        object_left_click(self.driver, locator, timeout=self.timeout)

    def type(self, locator, text: str):
        """输入文本"""
        log.debug(f"输入文本: {locator} -> {text}")
        object_send_keys(self.driver, locator, text, timeout=self.timeout)

    def text_of(self, locator) -> str:
        """获取元素文本"""
        return self.find(locator).text
